package com.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.constants.SuccessCodeEnum;
import com.example.domain.Dorm;
import com.example.service.DormService;
import com.example.vo.DormVo;
import com.example.vo.res.DormJsonResult;
import com.example.vo.res.JsonResult;

@Api("宿舍接口")
@RestController
@RequestMapping(value = "/dorms")
public class DormController {
    @Resource
    private DormService service;

    @ApiOperation(value = "获取列表", notes = "", response = DormJsonResult.class)
    @RequestMapping(value = { "" }, method = RequestMethod.GET)
    public JsonResult getUserList() {
        return new JsonResult(SuccessCodeEnum.SUCCESS, service.findAll());
    }

    @ApiOperation(value = "创建", response = DormJsonResult.class)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public JsonResult postUser(@RequestBody DormVo dorm) {
        Dorm dormdo = new Dorm();
        BeanUtils.copyProperties(dorm, dormdo);
        service.save(dormdo);
        return new JsonResult();
    }

    @ApiOperation(value = "获取详细信息", response = DormJsonResult.class)
    @ApiImplicitParam(name = "id", required = true, dataType = "Long",
        paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult getUser(@PathVariable Long id) {
        return new JsonResult(SuccessCodeEnum.SUCCESS, service.findOne(id));
    }

    @ApiOperation(value = "更新详细信息", response = DormJsonResult.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "ID", required = true,
            dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "dorm", value = "详细实体", required = true,
            dataType = "DormVo") })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public JsonResult putUser(@PathVariable Long id, @RequestBody DormVo dorm) {
        Dorm dormdo = service.findOne(id);
        BeanUtils.copyProperties(dorm, dormdo, "id");
        service.save(dormdo);
        return new JsonResult();
    }

    @ApiOperation(value = "删除用户", notes = "删除对象",
        response = DormJsonResult.class)
    @ApiImplicitParam(name = "id", value = "ID", required = true,
        dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        service.remove(id);
        return "success";
    }

}